﻿/*
 * Author: Branimir Topic (uxxx4663)
 * For: UC - ODD - Assignment 2B
 * 
 */

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;
using System.Data;
using System.Data.SqlClient;


namespace ARMS.BaseClasses
{
    public class BaseModel
    {
        protected static int errors { get; set; }

        #region members/methods

        protected SqlConnection connection;

        #endregion private members/methods

        public BaseModel()
        {
            connection = new System.Data.SqlClient.SqlConnection();
            connection.InfoMessage += new SqlInfoMessageEventHandler(OnInfoMessage);
        }
        #region protected
        protected void Connect()
        {
            connection.ConnectionString = "server=WIN-7P70M6R7N7F;" + "integrated security=sspi;database=ARMS;";
            connection.Open();

            if (connection.State != System.Data.ConnectionState.Open)
            {
                throw new Exception("Database connnecting error.");
            }
        }
        protected void Disconnect()
        {
            try
            {
                connection.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error closing database connection: " + e.ToString());
            }
        }
        protected virtual DataSet Open(string sql)
        {
            DataSet data = new DataSet();
            try
            {
                Connect();

                SqlDataAdapter adapter = new SqlDataAdapter();

                adapter.SelectCommand = new SqlCommand(sql, connection);
                adapter.Fill(data);
            }
            catch (Exception e)
            {
                throw new Exception("BaseModel.Open", e);
            }
            finally
            {
                if (connection.State == ConnectionState.Open)
                    Disconnect();
            }

            return data;
        }
        protected virtual DataTable OpenTable(string sql)
        {
            DataTable data = new DataTable();
            try
            {
                Connect();

                SqlDataAdapter adapter = new SqlDataAdapter();

                adapter.SelectCommand = new SqlCommand(sql, connection);
                adapter.Fill(data);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error:" + e.ToString());
                throw new Exception("BaseModel.OpenTable error", e);
            }
            finally
            {
                if (connection.State == ConnectionState.Open)
                    Disconnect();
            }

            return data;
        }
        protected virtual int Execute(string sql)
        {
            errors = 0;
            int result = -1;
            try
            {
                Connect();

                SqlCommand query = new SqlCommand(sql, connection);
                result = query.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error:" + e.ToString());
                throw new Exception("BaseModel.Execute error", e);
            }
            finally
            {
                if (connection.State == ConnectionState.Open)
                    Disconnect();
            }
            return result;
        }
        protected void OnInfoMessage(Object sender, SqlInfoMessageEventArgs e)
        {

        }
        #endregion
    }
}